package com.study.common.baidu.ai;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.study.common.baidu.ai.domain.AccessTokenResponse;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * 百度第三方AI内容审核平台工具类
 * 审核内容如下：
 * 1、用户的昵称、个性签名
 * 2、用户的头像
 * 3、用户发布活动内容、活动标题、活动图片
 * 4、用户发布的圈子图片、圈子文本
 */
@Slf4j
@Component
public class AiExamineUtils {

    /**
     * 这个根据官方规定：是默认的
     */
    private static final String grant_type = "client_credentials";

    /**
     * 对应自己服务应用的 API Key
     */
    private static final String client_id = "TegrGAPDuZYK02SyxvybzBGV";

    /**
     * 对应自己服务应用的 Secret Key
     */
    private static final String client_secret = "BboUuLT9NdmYizMnu51cHj0YbHnRrTga";

    /**
     * 获取鉴权访问令牌Access_token的请求地址：官方推荐使用post请求
     */
    private static final String accessTokenUrl = "https://aip.baidubce.com/oauth/2.0/token";

    /**
     * 内容审核平台 -- 文本审核接口: 官方推荐使用post请求
     */
    private static final String textReviewUrl = "https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined";

    /**
     * 内容审核平台 -- 图像审核接口: 官方推荐使用post请求
     */
    private static final String imageReviewUrl = "https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined";


    /**
     * 获取鉴权访问令牌
     *
     * @return
     */
    private String getAccessToken() {
        RestTemplate restTemplate = new RestTemplate();

        //设置头部信息
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Accept", "application/json");

        HttpEntity<String> entity = new HttpEntity<>("", headers);

        String url = accessTokenUrl + "?client_id=" + client_id + "&client_secret=" + client_secret + "&grant_type=" + grant_type;

        ResponseEntity<AccessTokenResponse> response = restTemplate.exchange(url, HttpMethod.POST, entity, AccessTokenResponse.class);

        return response.getBody().getAccess_token();

    }


    /**
     * 文本内容审核
     *
     * @param text 审核文本对象
     * @return true 审核通过，false 审核失败
     */
    @SneakyThrows
    public boolean textContentReview(String text) {

        RestTemplate restTemplate = new RestTemplate();

        String accessToken = getAccessToken();

        //设置头部信息
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Accept", "application/json");


        //构造请求实体
        HttpEntity<String> requestEntity = new HttpEntity<>("", headers);

        String url = textReviewUrl + "?access_token=" + accessToken + "&text=" + text;

        ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST,
                requestEntity, String.class);
        String responseBody = responseEntity.getBody();

        //使用JSON解析
        Gson gson = new Gson();
        JsonObject jsonObject = gson.fromJson(responseBody, JsonObject.class);
        String conclusion = jsonObject.get("conclusion").getAsString();

        if (!conclusion.equals("合规")) {
            return false;
        }
        return true;
    }


    /**
     * 图片内容审核
     *
     * @param imageUrl 审核图片访问地址
     * @return
     */
    public boolean imageReview(String imageUrl) {

        RestTemplate restTemplate = new RestTemplate();

        String accessToken = getAccessToken();

        //设置头部信息
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Accept", "application/json");


        //构造请求实体
        HttpEntity<String> requestEntity = new HttpEntity<>("", headers);

        String url = imageReviewUrl + "?access_token=" + accessToken + "&imgUrl=" + imageUrl + "&imgType=" + "0";

        ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
        String responseBody = responseEntity.getBody();

        //使用JSON解析
        Gson gson = new Gson();
        JsonObject jsonObject = gson.fromJson(responseBody, JsonObject.class);
        String conclusion = jsonObject.get("conclusion").getAsString();

        if (!conclusion.equals("合规")) {
            return false;
        }
        return true;
    }

    public static void main(String[] args) {
        AiExamineUtils aiUtils = new AiExamineUtils();
        boolean b = aiUtils.textContentReview("你好");
        System.out.println("b = " + b);

        boolean b1 = aiUtils.imageReview("http://120.78.172.229:28045/group1/M00/00/0A/Cl4sW2TciLSAKgbPAACgBVbUvVQ039.jpg");
        System.out.println("b1 = " + b1);
    }


}

